package com.fansl.allround.open.invoker;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.fansl.allround.open.api.constant.JuHeApiConstant;
import com.fansl.allround.open.api.constant.enums.juhe.NewsTypeEnum;
import com.fansl.allround.open.api.entity.juhe.JeHeResult;
import com.fansl.allround.open.api.entity.juhe.NewsHeadlinesResult;
import com.fansl.allround.open.api.entity.juhe.SmsResult;
import com.fansl.allround.open.api.entity.juhe.WeatherResult;
import com.fansl.allround.open.config.JuHeProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author fansl
 * @Description: 聚合查询API
 * @date 2019/9/9 18:01
 */
@Slf4j
@Component
@AllArgsConstructor
public class JuHeApiInvoker {
    private final JuHeProperties properties;
    /**
     * 聚合api-查询新闻头条
     * @param newsType
     * @return
     */
    public JeHeResult<NewsHeadlinesResult> getNewsHeadlines(NewsTypeEnum newsType){
        try {
            if(newsType==null){
                newsType = NewsTypeEnum.Top;
            }
            String url = String.format(JuHeApiConstant.News_Headlines,
                    newsType.getName(),
                    properties.getNewsHeadlinesAppKey()) ;
            HttpResponse response = HttpUtil.createGet(url).execute();
            JeHeResult result = JSONObject.parseObject(response.body(),JeHeResult.class);
            NewsHeadlinesResult newsHeadlinesResult =
                    JSONObject.parseObject(JSONObject.toJSONString(result.getResult()),NewsHeadlinesResult.class);
            result.setResult(newsHeadlinesResult);
            log.info("[聚合api-查询新闻头条],api返回结果：{}",response.body());
            return result;
        }catch (Exception e){
            log.error("[聚合api-查询新闻头条]异常,信息：{}",e.getMessage(),e);
        }
        return new JeHeResult<>();
    }

    /**
     * 聚合api-查询天气预报
     * @param cityName
     * @return
     */
    public JeHeResult<WeatherResult> getWeather(String cityName){
        try {
            String url = String.format(JuHeApiConstant.Weather,
                    cityName,
                    properties.getWeatherAppKey()) ;
            HttpResponse response = HttpUtil.createGet(url).execute();
            JeHeResult result = JSONObject.parseObject(response.body(),JeHeResult.class);
            WeatherResult weatherResult =
                    JSONObject.parseObject(JSONObject.toJSONString(result.getResult()),WeatherResult.class);
            result.setResult(weatherResult);
            log.info("[聚合api-查询天气预报],api返回结果：{}",response.body());
            return result;
        }catch (Exception e){
            log.error("[聚合api-查询天气预报]异常,信息：{}",e.getMessage(),e);
        }
        return new JeHeResult<>();
    }

    /**
     * 聚合api-查询天气预报-支持的城市
     * @return
     */
    public JeHeResult<Object> getWeatherSupportCity(){
        try {
            String url = String.format(JuHeApiConstant.Weather_Support_City,
                    properties.getWeatherAppKey()) ;
            HttpResponse response = HttpUtil.createGet(url).execute();
            log.info("[聚合api-查询天气预报-支持的城市],api返回结果：{}",response.body());
            return JSONObject.parseObject(response.body(),JeHeResult.class);
        }catch (Exception e){
            log.error("[聚合api-查询天气预报-支持的城市]异常,信息：{}",e.getMessage(),e);
        }
        return new JeHeResult<>();
    }

    /**
     * 聚合api-查询天气种类列表
     * @return
     */
    public JeHeResult<Object> getWeatherType(){
        try {
            String url = String.format(JuHeApiConstant.Weather_Type,
                    properties.getWeatherAppKey()) ;
            HttpResponse response = HttpUtil.createGet(url).execute();
            log.info("[聚合api-查询天气种类列表],api返回结果：{}",response.body());
            return JSONObject.parseObject(response.body(),JeHeResult.class);
        }catch (Exception e){
            log.error("[聚合api-查询天气种类列表]异常,信息：{}",e.getMessage(),e);
        }
        return new JeHeResult<>();
    }

    public JeHeResult<SmsResult> sendSmsCode(String mobile, int tplId, String tplValue) {
        try {
            String url = String.format(JuHeApiConstant.SMS,mobile,tplId,URLUtil.encode(tplValue,"utf-8"),properties.getSmsAppKey());
            HttpResponse response = HttpUtil.createGet(url).execute();
            JeHeResult result = JSONObject.parseObject(response.body(),JeHeResult.class);
            SmsResult smsResult =
                    JSONObject.parseObject(JSONObject.toJSONString(result.getResult()),SmsResult.class);
            result.setResult(smsResult);
            log.info("[聚合api-发送短信],api返回结果：{}",response.body());
            return result;
        }catch (Exception e){
            log.error("[聚合api-发送短信]异常,信息：{}",e.getMessage(),e);
        }
        return new JeHeResult<>();
    }
}
